/*
 * @Author: zxhuan 
 * @Date: 2017-12-08 15:23:02 
 * @blog: http//:www.zxhuan.com 
 * @Last Modified time: 2017-12-08 15:23:02 
 */
import { JSEncrypt } from 'jsencrypt';
import { PUBLICKEY } from '@/mock/defalut';
/**
 * js的公共函数库
 */
const Func = {
  quicksort(arr, left, right) {
    if (left < right) {
      var key = arr[left];  // 基准数
      var first = left;   // 开始位置
      var last = right;  // 结束位置

      while (first < last) {
        while (first < last && arr[last] >= key) {
          last--;
        }

        arr[first] = arr[last];

        while (first < last && arr[first] <= key) {
          first++;
        }

        arr[last] = arr[first];
      }
      arr[first] = key;
      this.quicksort(arr, left, first - 1);
      this.quicksort(arr, first + 1, right);
    }
  },
  // 深拷贝对象或者数组
  deepCopy(o) {
    if (o instanceof Array) {
      var n = [];
      for (var i = 0; i < o.length; ++i) {
        n[i] = this.deepCopy(o[i]);
      }
      return n;
    } else if (o instanceof Object) {
      var k = {};
      for (var j in o) {
        k[j] = this.deepCopy(o[j]);
      }
      return n;
    } else {
      return o;
    }
  },
  /**
   * 发送验证码后的倒计时，防治重复点击
   * @param {*} tartget 目标HTML
   * @param {*} _this  this
   * @param {*} clickBool 布尔值的名字
   * @param {*} innerText 切换回原来的文字
   * @param {*} timeNum 默认的时间
   */
  countdown(tartget, _this, clickBool, innerText, timeNum = 60) {
    let timers = setInterval(() => {
      timeNum--;
      tartget.innerHTML = timeNum + 's';
      if (timeNum <= 0) {
        tartget.innerHTML = innerText;
        clearInterval(timers);
        _this[clickBool] = false;
      }
    }, 1000);
  },
  // Js参数RSA加密传输,密码加密
  PasswordEncrypt(data) {
    let publicKey = PUBLICKEY;
    let encrypt = new JSEncrypt();
    encrypt.setPublicKey(publicKey);
    // ajax请求发送的数据对象
    let sendData = new Object();
    // 将data数组赋给ajax对象
    sendData = encrypt.encrypt(data);
    return sendData;
  }
};
export {
  Func
};